#!/bin/bash

# Define the paths to the CSV files
csv1=$1
csv2=$2

# Define the path to the output CSV file
folder_path=$(dirname "$csv1")
basename1=$(basename ${csv1%.*})
basename1=${basename1#"output_syntcomp23"}
basename2=$(basename ${csv2%.*})
basename2=${basename2#"output_syntcomp23"}
output=$folder_path/diff_"$basename1"_VS_"$basename2".csv

# Print the header to the output CSV file
echo "Folder, Filename, Result1, Result2" > $output

# Extract the number of lines in the first CSV file
lines=$(wc -l < $csv1)

# Iterate over each line in the CSV files
for ((i=2; i<=$lines; i++))
do
    # Extract the Result column from each CSV file
    result1=$(sed -n ${i}p $csv1 | cut -d, -f3)
    result2=$(sed -n ${i}p $csv2 | cut -d, -f3)

    # Compare the Results
    if [ "$result1" != "$result2" ]; then
        # If the Results are different, extract the Folder and Filename columns
        folder=$(sed -n ${i}p $csv1 | cut -d, -f1)
        filename=$(sed -n ${i}p $csv1 | cut -d, -f2)

        # Print the results to the output CSV file
        echo "$folder, $filename, $(printf "%-14s" "$result1"), $(printf "%-14s" "$result2")" >> $output
    fi
done